//
//  KCRNVC.m
//  Golden
//
//  Created by king on 2022/11/30.
//  Copyright © 2022 KC. All rights reserved.
//

#import "KCKit.h"
#import "KCRNVC.h"
#import "KCRLCell.h"
#import "KCTreasureDetailVC.h"
#import "KCTreasureModel.h"
#import "KCStation.h"
#import "KCTreasureModel.h"

@interface KCRNVC ()<UITableViewDelegate,UITableViewDataSource,KCStationDelegate>

@property (nonatomic,strong)KCStation *station;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray <KCTreasureModel *>*dataArray;

@end

@implementation KCRNVC

- (KCStation *)station {
    if (!_station) {
        _station = [KCStation RegisterStation:KHttpStationHomeUsers delegate:self];
    }
    return _station;
}

- (id)dispatch:(KCCargo *)cargo {
    
    if (cargo.cargoID == 0x01) {
        NSDictionary *dic = (NSDictionary *)cargo.cargoObj;
        KCLog(@"0x01 = %@",dic);
        if ([dic[HttpResult]integerValue] == HTTPResultCode_100) {
            WEAK_SELF;
            [_dataArray removeAllObjects];
            NSDictionary *dataDic = dic[HttpResultData];
            NSArray *daArray = dataDic[HttpResultList];
            [daArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                KCTreasureModel *model = [KCTreasureModel modelWithDictionary:obj];
                [weakSelf.dataArray addObject:model];
            }];
            [_tableView reloadData];
        }
    }
    return nil;
    
}


#pragma mark - LifeCycle

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ShareApplication setStatusBarHidden:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [ShareApplication setStatusBarHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.KCTitleLable.text = @"题材介绍";
    _dataArray = [NSMutableArray array];
    self.view.backgroundColor = WHITE_COLOR;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navigationHeight, SCREEN_WIDTH, SCREEN_HEIGHT - self.navigationHeight)
                                             style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = WHITE_COLOR;
    _tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    [self requestArt];
}

- (void)requestArt {
    NSDictionary *params = @{@"type":@"1",@"keywords":_IDString};
    NSDictionary *KCHttp_Obj = @{KC_URL:@"https://m2.icangyu.com/icyApi/carousel_list",KC_PARAMS_DATA:params};
    [self.station sendCargo:KHttpStation_Post cargoID:0x01 cargoObj:KCHttp_Obj];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KCRLCell *cell = [KCRLCell cellWithTableView:tableView];
    cell.imgStr = _dataArray[indexPath.row].pic_header;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return IMAGE_HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KCTreasureDetailVC *detail = [[KCTreasureDetailVC alloc]init];
    detail.artID = _dataArray[indexPath.row].ID;
    [self.navigationController pushViewController:detail animated:YES];
}

@end
